package com.sx.manage.controller.supplier;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sx.common.util.StringUtil;
import com.sx.manage.controller.base.MBaseController;
import com.sx.manage.service.supplier.IMSupplierService;
import com.sx.manage.service.supplier.MSupplierService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.model.constants.MSupplierStatus;
import com.sx.model.manage.mybatis.form.business.MBusinessForm;
import com.sx.model.manage.mybatis.vo.business.MBusinessVO;
import com.sx.model.manage.mybatis.vo.consumer.MVerifOpenAccountVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.result.ResultVerifyVO;

/**
 * 供应商信息 controller
 * 
 * @author lenovo
 *
 */
@Controller
@RequestMapping("/businessInfo")
public class MSupplierController extends MBaseController {
	/**
	 * 日志
	 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "supplierServiceImpl")
	private IMSupplierService supplierServiceImpl;

	@Resource(name = "supplierService")
	private MSupplierService supplierService;

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "businessIndex")
	public ViewAndModel businessIndex() {
		_log.debug("跳转到商户列表界面 businessIndex ");

		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("function/tenantInfo/tenantList");

		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */

	@RequestMapping(value = "toBusinessAdd")
	public ViewAndModel toBusinessAdd() {
		_log.debug("跳转到商户添加界面 toBusinessAdd ");

		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("function/tenantInfo/addTenant");

		// 获取角色编号
		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "toBusinessEdit/{id}")
	public ViewAndModel toBusinessEdit(@PathVariable Long id) {
		_log.debug("跳转到商户编辑界面 toBusinessEdit ");

		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("function/tenantInfo/editTenant");

		ResultObjectVO<MBusinessVO> objResult = this.supplierServiceImpl.getSupplierById(id);

		model.addObject("businessVO", objResult.getObjVo());
		return model;
	}

	/**
	 * 跳转到 角色列表用户
	 */
	@RequestMapping(value = "toBusinessView/{id}")
	public ViewAndModel toBusinessView(@PathVariable Long id) {
		_log.debug("跳转到商户查看界面 toBusinessView ");

		ViewAndModel model = new ViewAndModel(request, cache);
		model.setViewName("function/tenantInfo/viewTenant");

		ResultObjectVO<MBusinessVO> objResult = this.supplierServiceImpl.getSupplierById(id);

		model.addObject("businessVO", objResult.getObjVo());
		return model;
	}

	/**
	 * 获取翻页信息
	 * 
	 * @param roleVO
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value = "businessPage")
	@ResponseBody
	public ResultListVO<MBusinessVO> businessPage(MBusinessForm form) {
		_log.debug("跳转到商户列表数据 businessPage ");
		return this.supplierServiceImpl.getSupplierPage(form);

	}

	/**
	 * 获取集合信息
	 * 
	 * @param roleVO
	 * @param start
	 * @param length
	 * @return
	 */
	@RequestMapping(value = "businessList")
	@ResponseBody
	public ResultListVO<MBusinessVO> businessList(HttpServletRequest request, HttpServletResponse response) {
		String str = request.getQueryString();
		String q = str.split("&")[0];
		MBusinessForm form = new MBusinessForm();
		try {
			if (null != q) {
				String company_name = new String(q.split("=")[1].getBytes("iso8859_1"));
				form.setBusiness_name(java.net.URLDecoder.decode(company_name, "utf-8"));
			}
		} catch (Exception e) {
			_log.error("获取商户信息列表异常error={}",e);
			return new ResultListVO<>(false, null, "数据解析错误");
		}
		return this.supplierServiceImpl.getSupplierList(form);
	}

	/**
	 * 保存供应商信息
	 * 
	 * @param form
	 * @return
	 */
	@RequestMapping(value = "businessSave")
	@ResponseBody
	public ResultBooleanVO businessSave(MBusinessForm form) {
		return this.supplierServiceImpl.saveSupplier(form, this.loginConsumer());
	}

	/**
	 * 删除供应商信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "deleteBusinessById")
	@ResponseBody
	public ResultBooleanVO deleteBusinessById(Long id) {

		return this.supplierServiceImpl.deleteSupplier(id, this.loginConsumer());
	}

	/**
	 * 上线
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "onLineStatus")
	@ResponseBody
	public ResultBooleanVO onLineStatus(Long id) {

		return this.supplierServiceImpl.updateSupplierStatus(id, MSupplierStatus.SUPPLIER_ONLINE, this.loginConsumer());
	}

	/**
	 * 下线
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "offLineStatus")
	@ResponseBody
	public ResultBooleanVO offLineStatus(Long id) {

		return this.supplierServiceImpl.updateSupplierStatus(id, MSupplierStatus.SUPPLIER_OFFLINE, this.loginConsumer());
	}

	@RequestMapping(value = "openBusinessAccounts")
	@ResponseBody
	public ResultVerifyVO<MVerifOpenAccountVO> openBusinessAccounts(String comIds){
		if (StringUtil.isBlank(comIds)) {
			return null;
		}
		String[] comids = comIds.split(",");
		Long[] companyIds = new Long[comids.length];
		for (int i = 0; i < comids.length; i++) {
			companyIds[i] = Long.valueOf(comids[i]);

		}
		return supplierService.account(companyIds);
	}

}
